#coding=utf-8
# 导入模板
import pygame
from pygame.locals import *

import time
import random

# 子弹类
class Bullet(object):
    # 构造方法
    def __init__(self, plane):
        # False为敌机的子弹
        self.side = plane.side
        
        
        if self.side:
            # 坐标
            self.x = plane.x + 40
            self.y = plane.y - 20
            # 图片
            self.image = pygame.image.load("./images/player_bullet.png")
        else:
            # 坐标
            self.x = plane.x + 22
            self.y = plane.y + 37
            self.image = pygame.image.load("./images/enemy_bullet.png")
            
    # 子弹移动
    def move(self):
        if self.side:
            self.y -= 4
        else:
            self.y += 4
            

# 飞机类
class Plane(object):
    # 构造函数
    def __init__(self, side):
        # 生死
        self.life = True
        # False为敌机
        self.side = side
        if self.side:
            # 坐标
            self.x = 160
            self.y = 400
            # 图片
            self.image = pygame.image.load("./images/player_life.png")
        else:
            # 坐标
            self.x = 190
            self.y = 100
            # 图片
            self.image = pygame.image.load("./images/enemy_life.png")
            
        # 子弹列表
        self.bulletList = []
        
        # 敌机左右移动方向
        self.leftright = True
    # 玩家键盘控制移动
    def player(self, key):
        if self.life :
            if key == K_UP :
                if self.y > 0 :
                    self.y -= 20

            elif key == K_DOWN :
                if self.y < 580 :
                    self.y += 20

            elif key == K_LEFT :
                if self.x > 0 :
                    self.x -= 20

            elif key == K_RIGHT :
                if self.x < 320:
                    self.x += 20
                    
            elif key == K_SPACE :
                # 空格发射子弹
                bullet = Bullet(self)
                self.bulletList.append(bullet)
                
    
    
    
    
    # 敌机自动移动，自动发射子弹
    def enemy(self):
        if self.life :
            # 上下左右移动
            direction = random.randint(1, 400)
            if direction > 0 and direction <= 50 :
                # 上
                if self.y > 0 :
                    self.y -= 10
            elif direction > 100 and direction <= 150 :
                # 下
                if self.y < 660 :
                    self.y += 10
            elif direction > 200 and direction <= 210 :
                # 左
                if self.x > 0 :
                    self.x -= 10
            elif direction > 300 and direction <= 310 :
                # 右
                if self.x < 370 :
                    self.x += 10
            
            if self.x <= 0 :
                self.leftright = True
            if self.x >= 370 :
                self.leftright = False
            if self.leftright :
                self.x += 5
            else:
                self.x -= 5
            # 发射子弹
            launch = random.randint(0, 100)
            for i in [0, 25, 50, 75, 100] : 
                if i == launch:
                    bullet = Bullet(self)
                    self.bulletList.append(bullet)
            
    # 显示飞机和子弹
    def display(self, window):
        # 将飞机图片添加到窗体
        window.blit(self.image, (self.x, self.y))
        
        for bullet in self.bulletList:
            # 子弹移动
            bullet.move()
            # 将子弹图片添加到窗体
            window.blit(bullet.image, (bullet.x, bullet.y))
            if bullet.y < 0 or bullet.y > 700 :
                # 把不在地图中的子弹删除
                self.bulletList.remove(bullet)

    def lifedead(self, plane):
        for bullet in self.bulletList:
            x = bullet.x - plane.x
            y = bullet.y - plane.y
            if self.side:
                if x > -22 and x < 51 and y >-22 and y < 39:
                    
                    plane.life = False
                    plane.image = pygame.image.load("./images/enemy_dead.png")
                    
                    self.life = False
            else:
                if x > -9 and x < 100 and y > -21 and y < 124:
                    plane.life = False
                    plane.image = pygame.image.load("./images/player_dead.png")
                    
                    self.life = False
                    
    def removeBulletList(self):
        for bullet in self.bulletList:
            self.bulletList.remove(bullet)
# 飞机大战游戏
if __name__ == '__main__':
    # 创建窗体，设置大小
    window = pygame.display.set_mode((420, 700), 0, 32)
    # 加载背景图片
    background = pygame.image.load("./images/background.png")
    # 创建玩家飞机
    player = Plane(True)
    # 创建玩家飞机
    enemy = Plane(False)
    
    while True:
        # 将背景图片添加到窗体
        window.blit(background, (0, 0))

        # 显示
        player.display(window)
        enemy.display(window)
        # 玩家键盘
        for event in pygame.event.get():
            if event.type == QUIT :
                exit()
            elif event.type == KEYDOWN :
                player.player(event.key)
        # 敌机自动
        enemy.enemy()
        # 更新窗口
        pygame.display.update()
        
        # 睡眠
        if player.life and enemy.life :
            # 检查生死
            if player.life :
                player.lifedead(enemy)
            if enemy.life :
                enemy.lifedead(player)
            time.sleep(0.01)
        else:
            player.removeBulletList()
            enemy.removeBulletList()
            time.sleep(1)
            # 复活
            if not player.life :
                player = Plane(True)
            if not enemy.life :
                enemy = Plane(False)
            
        